<template>
  <div class="login-container">
    <el-form
      class="login-form"
      autoComplete="on"
      :model="loginForm"
      :rules="loginRules"
      ref="loginForm"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">能源设备管理平台</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          name="username"
          type="text"
          v-model="loginForm.username"
          autoComplete="on"
          placeholder="用户名"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          name="password"
          :type="passwordType"
          @keyup.enter.native="handleLogin"
          maxlength="12"
          v-model="loginForm.password"
          autoComplete="off"
          placeholder="密码"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye" v-if="this.passwordType === 'password'" />
          <svg-icon icon-class="openeye" v-else />
        </span>
      </el-form-item>
      <el-row
        :gutter="10"
        type="flex"
        justify="space-between"
        v-show="showCaptcha"
      >
        <el-col :span="14">
          <el-form-item prop="captchaCode">
            <span class="svg-container">
              <svg-icon icon-class="captcha" style="color:#304156;" />
            </span>
            <el-input
              name="captchaCode"
              type="text"
              maxlength="4"
              v-model="loginForm.captchaCode"
              autoComplete="off"
              placeholder="验证码"
            />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item style="overflow:hidden;">
            <img
              @click="changeCodeImg()"
              :src="codeImg"
              class="codeimg"
              title="点击切换"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-button
        id="showLogin"
        :disabled="disabled"
        type="primary"
        :loading="loading"
        @click.native.prevent="handleLogin"
        >登 录</el-button
      >
    </el-form>
    <div class="bg"></div>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
import { mapActions } from 'vuex'
import { waves } from '@/utils/waves'

export default {
  components: {},
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('请输入正确的6-12位密码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        captchaCode: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false,
      showCaptcha: false,
      disabled: false,
      codeImg: process.env.BASE_API + 'login/valid'
    }
  },
  mounted() {
    this.animationStart()
  },
  methods: {
    ...mapActions(['generateRoutes']),
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('login', this.loginForm)
            .then(({ data }) => {
              // console.log(data)
              global.myFunctions = data.functions
              // 在登录时把按钮权限数组保存到本地缓存
              sessionStorage.setItem('Authorization', `Bearer ${data.token}`)
              this.generateRoutes().then(() => {
                this.animationEnd()
                this.$router.addRoutes(this.$store.getters.addRouters)
                this.loading = false
                this.$router.push({ path: '/' })
              })
            })
            .catch(e => {
              this.$message({
                showClose: true,
                message: e.message,
                type: 'error'
              })
              const code = e.response.data.code
              if (code === 1006) {
                this.disabled = true
              }
              const times = e.response.data.data.times
              if (times > 2) {
                this.showCaptcha = true
                this.changeCodeImg()
              }
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    changeCodeImg() {
      this.codeImg = process.env.BASE_API + 'login/valid?' + Math.random()
    },
    isIE() {
      if (!!window.ActiveXObject || 'ActiveXObject' in window) {
        return true
      } else {
        return false
      }
    },
    isIE11() {
      if (/Trident\/7\./.test(navigator.userAgent)) {
        return true
      } else {
        return false
      }
    },
    animationStart() {
      const bgIE = document.getElementsByClassName('bg')
      if (this.isIE() || this.isIE11()) {
        bgIE[0].style.backgroundImage =
          "url('src/assets/bg_images/loginbg_IE.png')"
      } else {
        waves()
      }
    },
    animationEnd() {
      const waves = document.getElementById('waves')
      if (this.isIE() || this.isIE11()) {
        return true
      } else {
        waves.remove()
      }
    }
  },
  created() {
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #c9e0fb;
$light_gray: #304156;

canvas {
  position: absolute;
  z-index: 1;
}

@supports (-webkit-mask: none) and (caret-color: $light_gray) {
  .login-container .el-input input {
    color: $light_gray;
    // &::first-line {
    //   color: $light_gray;
    // }
    &::-webkit-input-placeholder {
      color: $light_gray;
    }
    &::-moz-placeholder {
      /* Mozilla Firefox 19+ */
      color: $light_gray;
    }
    &:-moz-placeholder {
      /* Mozilla Firefox 4 to 18 */
      color: $light_gray;
    }
    &:-ms-input-placeholder {
      /* Internet Explorer 10-11 */
      color: $light_gray;
    }
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    width: 80%;
    font-size: 1.6vh;
    input {
      background: transparent;
      border: 0vh;
      -webkit-appearance: none;
      border-radius: 0vh;
      padding: 0vh 0.5vh 0vh 1.5vh;
      font-size: 1.6vh;
      color: $light_gray;
      height: 4.8vh;
      line-height: 4.8vh;
      caret-color: $light_gray;
      vertical-align: middle;
      &:-webkit-autofill {
        box-shadow: 0 0 0vh 100vh $bg inset !important;
        -webkit-box-shadow: 0 0 0vh 100vh $bg inset !important;
        -webkit-text-fill-color: $light_gray !important;
      }
    }
  }
  .el-form-item {
    border: 0.1vh solid rgba(255, 255, 255, 0.1);
    background: $bg;
    border-radius: 0.5vh;
    color: $light_gray;
    margin-bottom: 2.6vh;
  }
  .el-form-item__content {
    line-height: 3.6vh;
    font-size: 1.6vh;
  }
  .el-form-item__error {
    font-size: 1.2vh;
    padding-top: 0.4vh;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #409eff;
$dark_gray: #304156;
$light_gray: #e9f3fc;

.login-container {
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 40vh;
    padding: 3vh 3vh 1.5vh 3vh;
    margin: 32vh auto;
    background: url(../../assets/bg_images/loginkuang.png) no-repeat;
    background-size: 100%;
    background-color: $light_gray;
    border-radius: 1vh;
    z-index: 9999;
  }
  .tips {
    font-size: 1.4vh;
    color: #fff;
    margin-bottom: 1vh;
    span {
      &:first-of-type {
        margin-right: 1.6vh;
      }
    }
  }
  .bg {
    position: fixed;
    height: 100%;
    width: 100%;
    background: url(../../assets/bg_images/loginbg.png) no-repeat center center
      fixed;
    background-size: 100% 100%;
  }
  .svg-container {
    padding: 0.6vh 0.5vh 0.6vh 1.5vh;
    color: $dark_gray;
    vertical-align: middle;
    width: 3vh;
    display: inline-block;
    font-size: 1.6vh;
    &_login {
      font-size: 2.2vh;
      padding-left: 1.2vh;
    }
  }
  .title-container {
    .title {
      font-size: 2.6vh;
      color: $light_gray;
      margin: 0vh auto 5.5vh auto;
      text-align: center;
      font-weight: bold;
    }
    .set-language {
      color: #fff;
      position: absolute;
      top: 0.5vh;
      right: 0vh;
    }
  }
  .show-pwd {
    position: absolute;
    right: 1vh;
    top: 0.7vh;
    font-size: 1.6vh;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 3.5vh;
    bottom: 2.8vh;
  }
  .codeimg {
    float: right;
    width: 100%;
    height: 4.8vh;
    cursor: pointer;
  }
  #showLogin {
    width: 100%;
    margin-bottom: 1vh;
    font-size: 1.6vh;
    height: 4.8vh;
    padding: 0vh;
    background-color: $bg;
    border-color: $bg;
    border-radius: 0.5vh;
  }
  #showLogin:hover {
    background-color: #54b2ff;
    border-color: #54b2ff;
  }
}
</style>
